package com.cardone.common.util;

import java.util.List;
import java.util.Set;

import lombok.Setter;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;

import com.cardone.common.Characters;
import com.google.common.collect.Lists;

/**
 * 字符串工具类
 *
 * @author yaohaitao
 */
public class StringUtils {
	/**
	 * 路径解析
	 */
	private static PathMatcher defaultPathMatcher;

	@Setter
	private static PathMatcher pathMatcher;

	static {
		final AntPathMatcher antPathMatcher = new AntPathMatcher();

		antPathMatcher.setCachePatterns(true);

		StringUtils.defaultPathMatcher = antPathMatcher;
	}

	/**
	 * 返回不为空的字符串
	 *
	 * @param strs
	 *          字符串集合
	 * @return 不为空的字符串
	 */
	public static String defaultIfBlank(final String... strs) {
		if (org.apache.commons.lang3.ArrayUtils.isEmpty(strs)) {
			return null;
		}

		for (final String str : strs) {
			if (org.apache.commons.lang3.StringUtils.isNotBlank(str)) {
				return str;
			}
		}

		return null;
	}

	/**
	 * 获取路径
	 *
	 * @param pathList
	 *          路径集合
	 * @param url
	 *          路径
	 * @return 路径
	 */
	public static String getPathForMatch(final List<String> pathList, final String url) {
		if (CollectionUtils.isEmpty(pathList)) {
			return null;
		}

		for (final String path : pathList) {
			if (org.apache.commons.lang3.StringUtils.equals(url, path)) {
				return path;
			}

			if (!StringUtils.getPathMatcher().isPattern(path)) {
				continue;
			}

			if (StringUtils.getPathMatcher().match(path, url)) {
				return path;
			}
		}

		return null;
	}

	/**
	 * 获取路径
	 *
	 * @param pathSet
	 *          路径集合
	 * @param url
	 *          路径
	 * @return 路径
	 */
	public static String getPathForMatch(final Set<String> pathSet, final String url) {
		if (CollectionUtils.isEmpty(pathSet)) {
			return null;
		}

		for (final String path : pathSet) {
			if (org.apache.commons.lang3.StringUtils.equals(url, path)) {
				return path;
			}

			if (!StringUtils.getPathMatcher().isPattern(path)) {
				continue;
			}

			if (StringUtils.getPathMatcher().match(path, url)) {
				return path;
			}
		}

		return null;
	}

	/**
	 * 获取路径比较
	 *
	 * @return 路径比较
	 */
	public static PathMatcher getPathMatcher() {
		if (StringUtils.pathMatcher == null) {
			return StringUtils.defaultPathMatcher;
		}

		return StringUtils.pathMatcher;
	}

	/**
	 * 比较
	 *
	 * @param pathList
	 *          路径集合
	 * @param url
	 *          路径
	 * @return 比较结果
	 */
	public static boolean matchList(final List<String> pathList, final String url) {
		return org.apache.commons.lang3.StringUtils.isNotBlank(StringUtils.getPathForMatch(pathList, url));
	}

	/**
	 * 比较
	 *
	 * @param paths
	 *          路径集合
	 * @param url
	 *          路径
	 * @return 比较结果
	 */
	public static boolean matchs(final String paths, final String url) {
		final List<String> pathList = Lists.newArrayList(org.apache.commons.lang3.StringUtils.split(paths, Characters.comma.stringValue()));

		return StringUtils.matchList(pathList, url);
	}
}
